package cn.itsource.controller;

import cn.hutool.core.lang.UUID;
import cn.itsource.config.OssProperties;
import cn.itsource.constants.ErrorCode;
import cn.itsource.result.JSONResult;
import cn.itsource.vo.UploadVO;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private OssProperties ossProperties;
    @PostMapping("/upload")
    public JSONResult uploadImg(@RequestParam("fileName") MultipartFile multipartFile){
        JSONResult returnVo = new JSONResult();
        ArrayList<String> strings = new ArrayList<>();
        strings.add("image/png");
        strings.add("image/gif");
        strings.add("image/jpeg");
        if (strings.contains(multipartFile.getContentType())){
            try {
                //获取文件名
                String fileName = multipartFile.getOriginalFilename();//123.jpg
                //获取文件类型
                String fileType = fileName.substring(fileName.lastIndexOf("."));//.jpg
                //uuid
                String uuid = UUID.randomUUID().toString().replace("-","");//hu23g4hj2gb423b4hj2b3hj4b32
                //company_logo/hu23g4hj2gb423b4hj2b3hj4b32.jpg
                String realName = "company_logo/" + uuid + fileType;
                OSS build = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey());
                PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), realName, multipartFile.getInputStream());
                build.putObject(putObjectRequest);
                build.shutdown();//关闭OSSClient
                String imagePath = "https://" + ossProperties.getBucketName()+"."+ossProperties.getEndpoint()+"/"+realName;
                UploadVO vo = new UploadVO();
                vo.setOssKey(realName);
                vo.setFilePath(imagePath);
                return returnVo.success(vo);
            } catch (IOException e) {
                //发生异常
                e.printStackTrace();
                return returnVo.error("系统异常123");
            }
        }else {
            //文件格式错误，非jpg/png/gif
            return returnVo.error("文件格式错误");
        }
    }
    @PostMapping("/remove")
    public JSONResult remove(@RequestBody UploadVO uploadVO) {
        OSS oss = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey());
        /**
         * 第二个参数：就是除了域名以外，后面的路径
         * https://hrm0507.oss-cn-chengdu.aliyuncs.com/itsource/797db90c-b555-4eb0-8067-9cf6fb9816d6_111.jpg
         * 这里的key就是：itsource/797db90c-b555-4eb0-8067-9cf6fb9816d6_111.jpg
         */
        oss.deleteObject(ossProperties.getBucketName(), uploadVO.getOssKey());
        oss.shutdown();//关闭OSSClient
        return JSONResult.success("文件删除成功");
    }
}
